#ifndef DISTRIBUTION_UTILS_HPP
#define DISTRIBUTION_UTILS_HPP

#include <include/distributions.hpp>
#include <vector>
#include <math.h>

class DistributionUtils {

    typedef std::vector<int>::iterator iter_vec;

public:
    // initialPermNumber - serial number of an initial permutation
    // numberOfProcessedPerms - count of processed permutations
    // numberOfPermsToProcess - count of permutations to process
    static std::vector<Distributions> computeDistributions(std::vector<int> cpuIds,
                                                           int initialPermNumber, int countOfProcessedPerms,
                                                           int countOfPermsToProcess, int sizeOfPermutation) {
        std::vector<Distributions> distributions;

        int countOfUnprocessedPerms = countOfPermsToProcess - countOfProcessedPerms;// + initialPermNumber - 1;
        int serialNumOfInitialPerm = initialPermNumber + countOfProcessedPerms;

        int idsCount = 0;
        int countOfCpus = cpuIds.size();

        iter_vec cpuIdsEnd = cpuIds.end();
        for(iter_vec cpuIdIter = cpuIds.begin(); cpuIdIter != cpuIdsEnd; ++cpuIdIter) {
            Distributions distribution;
            distribution.cpuId = (*cpuIdIter);
            distribution.initialPermNumber = serialNumOfInitialPerm;
            distribution.sizeOfPermutation = sizeOfPermutation;
            int numOfPermsToProcess = trunc(countOfUnprocessedPerms / (countOfCpus - idsCount));
            distribution.countOfPemrsToProcess = numOfPermsToProcess;
            distributions.push_back(distribution);

            idsCount++;
            countOfUnprocessedPerms = countOfUnprocessedPerms - numOfPermsToProcess;
            serialNumOfInitialPerm = serialNumOfInitialPerm + numOfPermsToProcess;
        }

        return distributions;
    }
};

#endif // DISTRIBUTION_UTILS_HPP
